package com.gjy.spring.controller;

import com.gjy.spring.aspect.Timeout;
import com.gjy.spring.service.AsyncService;
import com.gjy.spring.util.TimeoutUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.Callable;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-02-17 16:10:54
 */
@RequestMapping("/async")
@RestController
public class AsyncController {

    @Resource
    private AsyncService asyncService;

    @RequestMapping("/hello")
    public Callable<String> hello(@RequestParam Long time) {
        return () -> {
            // 模拟耗时操作
            Thread.sleep(time); // 超过30秒会触发超时
            return "Success";
        };
    }

    @Timeout(timeout = 3000)
    @RequestMapping("/hello2")
    public String hello2(@RequestParam Long time) {
        return asyncService.hello2(time);
    }

    @RequestMapping("/hello3")
    public String hello3(@RequestParam Long time, @RequestParam Long t2) {
        return TimeoutUtil.timeout(time, t2, "超时");
    }

}
